﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BCGC.BLL.Helper;
using BCGC.BLL.BusinessObject.Resource;
using log4net;
using Jalal.Library.Utility;

public partial class Admin_Resource_Trailer_AddMoreDownloadFormat : AdminBasePage
{
    private int Id
    {
        get
        {
            if (ViewState["Id"] == null)
                ViewState["Id"] = 0;
            return (int)ViewState["Id"];
        }
        set { ViewState["Id"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int id = Id = 0;
            if (!string.IsNullOrEmpty(Request.QueryString["id"]) && int.TryParse(Request.QueryString["id"], out id))
            {
                Id = id;
            }
            else
                Response.Redirect(CustomSiteMap.Trailers);
        }
    }

    protected void Upload_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            try
            {
                if (!TrailerFormat.AllreadyFormatExists(ddlFormat.SelectedValue))
                {
                    bool uploadSuccess = false;
                    Trailer currentTrailer = Trailer.GetTrailerById(Id);

                    if (ddlFormat.SelectedValue != "None")
                    {
                        string path = TrailerFormat.UploadTrailerFormatFile(fluMediaFile.PostedFile);
                        if (!string.IsNullOrEmpty(path))
                        {
                            if (TrailerFormat.Create(Id, ddlFormat.SelectedValue, path))
                            {
                                uploadSuccess = true;
                                uccResponse.SetFailMessage("Trailer format uploaded");
                            }
                        }
                    }

                    if (!uploadSuccess)
                        uccResponse.SetFailMessage("Failed to upload new trailer format");
                }
                else
                    uccResponse.SetFailMessage("Selected format already exists");
            }
            catch (Exception ex)
            {
                // Write the log into handled exception list
                ILog log = LogHelper.GetHandledFileLogger("AddMoreDownloadFormat");
                if (log.IsErrorEnabled)
                    log.Error("Failed to add download format to trailer", ex);

                uccResponse.SetFailMessage("There was a problem in uploading trailer format");
            }
        }
    }
}
